﻿using Inovout.Models;
using Inovout.Security;
using System.Threading;

namespace Inovout.Repositories
{
    public class DataObjectRepository<TDataObject> : BaseRepository<TDataObject> where TDataObject : DataObject
    {
        public override void Save(TDataObject dataObject)
        {
            if (!Comparer.DefaultValueEquals(dataObject.Id))
            {
                dataObject.ModifiedUserName = Thread.CurrentPrincipal.Identity.Name;
            }
            base.Save(dataObject);
        }

        public override TDataObject FindById(object id)
        {
            TDataObject dataObject= base.FindById(id);
            dataObject.AccessedUserName = Thread.CurrentPrincipal.Identity.Name;
            base.Save(dataObject);
            return dataObject;
        }
        //public override IList<TDataObject> FindAll()
        //{
        //     return CreateCriteriaQuery()
        //        .Where(DataObject.IsInvalidPersistentProperty.Equals(false))
        //        .OrderByDesc(DataObject.CreatedTimePersistentProperty)
        //        .Execute();
        //}
        protected override CriteriaQuery<TDataObject> CreateCriteriaQuery()
        {
            return base.CreateCriteriaQuery().Where(DataObject.IsInvalidPersistentProperty.Eq(false))
                .OrderByDesc(DataObject.CreatedTimePersistentProperty);
        }
    }
}
